﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BL;
using BO;

namespace AnimalFarm.AuctionMarket_manage
{
    public partial class AddNewFarmer : Form
    {
        public AddNewFarmer()
        {
            InitializeComponent();
            txtMAFFID.Text = GetRandomCode();
        }

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        private string RandomString(int size, bool lowerCase)
        {
            StringBuilder strBuilder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                strBuilder.Append(ch);
            }
            if (lowerCase)
                return strBuilder.ToString().ToLower();
            return strBuilder.ToString();
        }

        //This code generates a code of length 10 with first 4 letters lowercase,
        //next 4 letters numbers, and last 2 letters as uppercase.
        public string GetRandomCode()
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append(RandomString(4, true));
            strBuilder.Append(RandomNumber(1000, 9999));
            strBuilder.Append(RandomString(2, false));
            return strBuilder.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string MAFFID = txtMAFFID.Text;
            string farmerName = txtFarmerName.Text;
            string farmName = txtFarmName.Text;
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            string address = txtAddress.Text;
            string phoneNumber = txtPhoneNumber.Text;
            string email = txtEmail.Text;

            if (farmerName.Length > 0 && farmName.Length > 0 && username.Length > 0 && password.Length > 0 && address.Length > 0)
            {
                B_Farmer bFarmer = B_Farmer.CreateInstance();
                
                Farmer_Info f = new Farmer_Info();
                
                f.MAFFLicenceID = MAFFID;
                f.FarmerName = farmerName;
                f.FarmName = farmName;
                f.Username = username;
                f.Password = password;
                f.Address = address;
                f.PhoneNumber = phoneNumber;
                f.Email = email;
                
                bool isEffected = bFarmer.CreateOneFarmer(f);

                if (isEffected == true)
                {
                    MessageBox.Show("Has created a new farmer successfully!", "Adding Farmer Completely", MessageBoxButtons.OK);
                    txtMAFFID.Text = GetRandomCode();
                    txtFarmerName.Text = "";
                    txtFarmName.Text = "";
                    txtUsername.Text = "";
                    txtPassword.Text = "";
                    txtPhoneNumber.Text = "";
                    txtAddress.Text = "";
                    txtEmail.Text = "";
                    txtFarmerName.Focus();
                }
                else
                {
                    MessageBox.Show("Cannot create a new farmer", "Missing Data", MessageBoxButtons.OK);
                }
            }
        }
    }
}
